Date.prototype.format = function(fmt) {
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)) {
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
}

var so={
    level:2
}
var vm = new Vue({
    el:"#gys",
    data:{
        pageNum:1,
        pageSize:7,
        //总条数
        total:0,
        //总页数
        pages:0,
        isFirstPage:false,
        isLastPage:false,
        hasPreviousPage:false,
        hasNextPage:false,
        //所有导航页号
        navigatepageNums:[],



        nameSp:"",
        levelname:"",
        condition:"",
        sort:0,
        supplier:0,
        wareslist:[],

        ck:0,
        box:[],
        boxlist:{},
        quantity:1,
        songDate:"",
        jbr:"",
        beizhu:"",
        xiaoji:0,
        heji:0,
        danjia:"",
        peisong:0,

    },
    methods:{
        gys(){
            axios({
                method: "post",
                url:"/purchase/nameSp",
            }).then(response=>{
                console.log(response)
                var listname=response.data.obj
                vm.nameSp=listname
            })
        },
        fl(){
            axios.post("/wares/getSort",so,{
                'Content-Type':'multipart/form-data'
            }).then(res=>{
                var listlevel=res.data.obj

                vm.levelname=listlevel
            }).catch(err=>{

            })
        },
        dianji(){
            if(this.supplier == 0){
                alert("请选择供货商")
            }else{
                $('#my-confirm').modal({
                    relatedElement: this,
                    onConfirm: function () {
                        // alert('你是猴子派来的逗比!')
                    },
                    onCancel: function () {
                        // alert('你不确定是不是猴子派来的逗比!')
                    }
                });
                this.ss(this.supplier,this.pageNum,this.pageSize)
            }
        },
        totalx(waresId){
            if(waresId!=null) {
                this.boxlist[waresId].total = this.boxlist[waresId].quantity2 * this.boxlist[waresId].Price
                this.xiaoji = 0
                for (var i in this.boxlist) {
                    this.xiaoji = this.xiaoji + this.boxlist[i].total
                }
            }
            this.heji=parseInt(this.xiaoji)+parseInt(this.peisong)
            this.ss(this.supplier,this.pageNum,this.pageSize)
        },
        ss(su,pageNum,pageSize){

                axios.post("/wares/getWaresList",{"pageNum":pageNum,"pageSize":pageSize,"condition":this.condition,"sort":this.sort,"supplier":su},{
                    'Content-Type':'multipart/form-data'
                }).then(res=>{
                    this.pageNum=parseInt(res.data.obj.pageNum);
                    this.pageSize=parseInt(res.data.obj.pageSize);
                    this.total=parseInt(res.data.obj.total);
                    this.pages=parseInt(res.data.obj.pages);
                    this.isFirstPage=res.data.obj.isFirstPage;
                    this.isLastPage=res.data.obj.isLastPage;
                    this.hasPreviousPage=res.data.obj.hasPreviousPage;
                    this.hasNextPage=res.data.obj.hasNextPage;
                    this.navigatepageNums=res.data.obj.navigatepageNums;
                    var wareslist=res.data.obj.list
                    vm.wareslist=wareslist
                    console.log(wareslist)


                }).catch(err=>{

                })


            },

        qian(){
            if(this.hasPreviousPage==true){
                // alert("上一页")
                //表示有上一页
                this.ss(this.supplier,this.pageNum-1,this.pageSize);
            }
        },
        //下一页
        hou(){
            if(this.hasNextPage==true){
                this.ss(this.supplier,this.pageNum+1,this.pageSize);
            }
        },
        tianjia(i,e){
            if ($(e.target).prop('checked')){
                var json={
                    waresName:this.wareslist[i].name,
                    waresId:this.wareslist[i].id,
                    unit:this.wareslist[i].unit,
                    Price:this.wareslist[i].rPrice,
                    quantity:this.wareslist[i].quantity2,
                    xiaoji:"",
                    quantity2:1,
                    ktuihuo:this.quantity,
                    total:this.wareslist[i].rPrice
                }

                this.boxlist[json.waresId]=json
                this.xiaoji=0
                for(var i in this.boxlist){
                    this.xiaoji=this.xiaoji+this.boxlist[i].total
                }
                this.heji=parseInt(this.xiaoji)+parseInt(this.peisong)
                console.log(json)

                console.log(this.boxlist)
            }else{
                delete this.boxlist[this.wareslist[i].id]
                console.log(this.boxlist)
            }

        },
        baocun(){
            var xz = document.getElementsByName("ljw");
            var checkNum = 0;
            for (var i = 0; i < xz.length; i++) {
                if (xz[i].checked) {
                    checkNum++;
                }
            }
            if(checkNum==0){
                alert("请添加购物清单")
            }else{
                if(this.songDate!=0 && this.jbr!=0){
                    this.zj()
                }else{
                    alert("请填写送货时间和经办人")
                }
            }
        },

        zj(){
            var xxlist=[]
            for (var i in this.boxlist){
                this.boxlist[i].xiaoji=this.boxlist[i].quantity*this.boxlist[i].Price
                xxlist.push(this.boxlist[i])
            }
            var d={
                supplierId:this.supplier,
                orderMoney:this.heji,
                paidMoney:"0.00",
                orderStaId:1,
                payStaId:10,
                asStaId:16,
                deliveryDate:this.songDate,
                single:getCookie(),
                createDate:new Date().format("yyyy-MM-dd hh:mm:ss"),
                remark:this.beizhu,
                woseId:this.ck
            }

            var json={
                purchaseOrder:d,
                list:xxlist
            }
            var date =new FormData()
            date.append('json',JSON.stringify(json))
            date.append("file",$("#wenjian")[0].files[0])
            console.log(date)
            axios.post("/purchase/addPo",date,{
                'Content-Type':'multipart/form-data'
            }).then(res=>{
                alert("成功")
                location.href="采购订单.html"
            }).catch(err=>{
                alert("失败")
            })
        }
    },

    mounted(){
        this.gys();
        this.fl();
    }
})

$(function () {
    $("#wenjian").change(function () {
        $("#tup").attr('src',URL.createObjectURL(this.files[0]))
    })
})





